package com.ark.frigate.dingtalk.ding;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.ark.frigate.dingtalk.ding.message.TextMessage;
import com.ark.frigate.dingtalk.utils.EnvironmentUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 异常报警
 * <p>
 * https://developers.dingtalk.com/document/app/custom-robot-access
 *
 * @author zengweilong
 */
@Slf4j
public class DingTalkTextMessage extends DingTalkMessage {

    public void sendTextEx(Throwable e) {
        sendCoreExMessage(StringUtils.EMPTY, StringUtils.EMPTY, e, StringUtils.EMPTY, ArrayUtils.EMPTY_STRING_ARRAY);
    }

    public void sendTextExMsg(Throwable e, String content) {
        sendCoreExMessage(StringUtils.EMPTY, StringUtils.EMPTY, e, content, ArrayUtils.EMPTY_STRING_ARRAY);
    }

    public void sendTextEx(String dingTalkUrl, String secret, Throwable e) {
        sendCoreExMessage(dingTalkUrl, secret, e, StringUtils.EMPTY, ArrayUtils.EMPTY_STRING_ARRAY);
    }

    public void sendTextEx(Throwable e, String[] phones) {
        sendCoreExMessage(StringUtils.EMPTY, StringUtils.EMPTY, e, StringUtils.EMPTY, phones);
    }

    public void sendTextMsg(String dingTalkUrl, String secret, Throwable e, String content) {
        sendCoreExMessage(dingTalkUrl, secret, e, content, ArrayUtils.EMPTY_STRING_ARRAY);
    }

    public void sendText(String content) {
        sendCoreTextMessage(StringUtils.EMPTY, StringUtils.EMPTY, content, ArrayUtils.EMPTY_STRING_ARRAY);
    }

    public void sendText(String dingTalkUrl, String secret, String content) {
        sendCoreTextMessage(dingTalkUrl, secret, content, ArrayUtils.EMPTY_STRING_ARRAY);
    }

    /**
     * 发送带异常栈的钉钉消息
     *
     * @param dingTalkUrl
     * @param secret
     * @param e
     * @param content
     * @param atMobiles
     */
    public void sendCoreExMessage(String dingTalkUrl, String secret, Throwable e, String content, String[] atMobiles) {
        StringBuilder textBuilder = getHead(content);
        getExtInfo(e, textBuilder);
        getExtCase(e, textBuilder);
        //getExtStack(e, textBuilder);
        sendCoreText(dingTalkUrl, secret, textBuilder.toString(), atMobiles);
    }

    /**
     * 发送带异常栈的钉钉消息
     *
     * @param dingTalkUrl
     * @param secret
     * @param content
     * @param atMobiles
     */
    public void sendCoreTextMessage(String dingTalkUrl, String secret, String content, String[] atMobiles) {
        StringBuilder textBuilder = getHead(content);
        sendCoreText(dingTalkUrl, secret, textBuilder.toString(), atMobiles);
    }

    /**
     * 发送文本消息
     *
     * @param dingTalkUrl
     * @param secret
     * @param content
     * @param atMobiles
     */
    private void sendCoreText(String dingTalkUrl, String secret, String content, String[] atMobiles) {
        content = StringUtils.substring(content, 0, 16000);
        TextMessage textMessage = new TextMessage();
        textMessage.setSecret(secret);
        textMessage.setDingTalkUrl(dingTalkUrl);
        textMessage.setContent(content);
        if (ArrayUtils.isEmpty(atMobiles)) {
            atMobiles = StringUtils.split(dingTalkConfig.getPhones(), ",");
        }
        textMessage.setAtMobiles(atMobiles);
        try {
            sendCoreMessage(textMessage);
        } catch (Exception ex) {
            log.error("钉消息发送失败={}", textMessage, ex);
        }
    }

    private StringBuilder getHead(String content) {
        StringBuilder textBuilder = new StringBuilder(getEnv()).append(StringUtils.SPACE)
                .append(EnvironmentUtil.getHostName())
                .append(" @[").append(EnvironmentUtil.getHostIp()).append("]")
                .append(StringUtils.LF).append(content).append(StringUtils.LF);
        return textBuilder;
    }

    private StringBuilder getExtInfo(Throwable e, StringBuilder content) {
        content.append("异常信息:").append(StringUtils.LF);
        content.append(e).append(StringUtils.LF);
        return content;
    }

    /**
     * Throwable 解析
     *
     * @param e
     * @param content
     *
     * @return
     */
    private StringBuilder getExtStack(Throwable e, StringBuilder content) {
        content.append("调用栈:").append(StringUtils.LF);
        StackTraceElement[] elements = e.getStackTrace();
        if (ArrayUtils.isEmpty(elements)) {
            return content;
        }
        for (StackTraceElement element : elements) {
            content.append(element).append(StringUtils.LF);
        }
        return content;
    }

    /**
     * Throwable 解析
     *
     * @param e
     * @param content
     *
     * @return
     */
    private StringBuilder getExtCase(Throwable e, StringBuilder content) {
        content.append("Cause:");
        if (e.getCause() == null) {
            content.append(e);
            return content;
        }
        content.append(e.getCause()).append(StringUtils.LF);
        StackTraceElement[] elements = e.getCause().getStackTrace();
        if (ArrayUtils.isEmpty(elements)) {
            return content.append(e);
        }
        for (StackTraceElement element : elements) {
            if (element.toString().contains(dingTalkConfig.getPackageName())) {
                content.append(element).append(StringUtils.LF);
            }
        }
        return content;
    }

}
